<?php

require "conf.php";
require_once "data/dao/BaseDAO.php";
require_once "application/user/User.php";
require_once "utils/Validate.php";
require_once "application/exception/SqlException.php";
require_once "services/FacebookProxy.php";

class UserDAO extends BaseDAO{
	
	/**
	 * searches for a user with the associated id.
	 * 
	 * @param int id
	 * @throws Exception
	 * @return User
	 */
	public function findUser($id){
		
		self::connect();
		$find = mysql_query("SELECT * FROM " . USER_TABLE . " WHERE id = '$id'");
		
		if (mysql_num_rows($find) == 0)
			throw new Exception("User does not Exist!");
			
		while($row = mysql_fetch_assoc($find)){
			$id = $row['id'];
			$firstName = $row['first_name'];
			$lastName = $row['last_name'];
			$wiiCode = $row['wii_code'];
			$picSmall = $row['picSmall'];
			$picBig = $row['picBig'];
			$location = $row['location'];
			$storeLocation = $row['storeLocation'];
			return new User($id, $firstName, $lastName, $picSmall, $picBig, $wiiCode, 
				$location, $storeLocation);
		}
	}
	
	/**
	 * returns an array of user ids
	 * 
	 * @throws SqlException id no users are present in database
	 */
	public function getAllUsers(){
		
		/* this will be
		/*
		 * array(array(id => value), array(id => value))
		 */
		$userIds = $this->queryForArray("select id from " . USER_TABLE);
		
		if (count($userIds) == 0)
			throw new SqlException("No users in the database!");
		
		// must be a better way of doing this
		$userIdsArray = array();
		foreach($userIds as $userId){
			array_push($userIdsArray, $userId['id']);
		}
		
		return $userIdsArray;
	}
	
	public function addUser($id){
		
		$fb = new FacebookProxy();
		$fbInfo = $fb->getFacebookUserInfo($id);

		$firstname = $fbInfo[0]['first_name'];
		$lastname = $fbInfo[0]['last_name'];
		$picSmall = urlencode($fbInfo[0]['pic_small']);
		$picBig = urlencode($fbInfo[0]['pic_big']);
		
		Validate::notNull($id);
		Validate::notNull($firstname);
		Validate::notNull($lastname);

		$values = array($id, $firstname, $lastname, $picSmall, $picBig);
	 
		$this->update("insert into " . USER_TABLE . "(id, first_name, last_name, " .
				"picSmall, picBig) values ('%s', '%s', '%s', '%s', '%s')", $values);

		return;										
	}
	
	public function updateUser($id, array $args){
		
		$user = $this->findUser($id);
		
		$firstname = $user->getFirstName();
		if($firstname == null){
			$firstname = $args['first_name'];
		}
		
		$lastname = $user->getLastName();
		if($lastname == null){
			$lastname = $args['last_name'];
		}
		
		$picSmall = urlencode($args['pic_square']);
		$picBig = urlencode($args['pic']);
		
		Validate::notNull($id);
		//Validate::notNull($firstname);
		//Validate::notNull($lastname);

		$values = array($firstname, $lastname, $picSmall, $picBig, $id);
		
		$this->update("update " . USER_TABLE . " set first_name = '%s', " .
				"last_name = '%s', picSmall = '%s', " .
				"picBig = '%s' where id = '%s'", $values);

		return;				
	}
	
	public function updateUserDetails($id, array $args){
		
		Validate::notNull($id);
		
		// check contents of arg are correct
		if(count($args) != 5){
			throw new Exception("incorrect number of args");
		}
		
		$keys = array_keys($args);
		if ($keys[0] != "first-name"){
			throw new Exception("first item in array should be 'first-name'");
		}
		
		// push user id onto args array
		array_push($args, $id);
		
		try{
			$this->update("update " . USER_TABLE . " set first_name = '%s', 
				last_name = '%s', location = '%s', wii_code = '%s', storeLocation = '%s' " .
				"where id = '%s'", $args);
			
		}catch (Exception $e){
			return false;
		}
		
		return true;
	}
	
	public function deleteUser($id){
		
		self::connect();
		mysql_query("DELETE FROM " . USER_TABLE . " WHERE id = '$id'");
	}
	
	public function addWiiCode($userId, $wiiCode){
		
		Validate::isValidWiiCode($wiiCode);
		
		self::connect();
		
		mysql_query("UPDATE " . USER_TABLE . " SET wii_code = '$wiiCode' WHERE " .
				"id = '$userId'");		
	}
	
	public function deleteWiiCode($userId){
		
		self::connect();
		
		mysql_query("UPDATE " . USER_TABLE . " SET wii_code = '' WHERE id" .
				" = '$userId'");
	} 
}
?>
